﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace anks.Components
{
    public class Component
    {

        public class ComponentParam
        {
            protected string paramValue { get; set; }
            public bool asBool
            {
                get
                {
                    return Convert.ToBoolean(paramValue);
                }
                set
                {
                    paramValue = Convert.ToString(value);
                }
            }
            public int asInt
            {
                get
                {
                    return Convert.ToInt32(paramValue);
                }
                set
                {
                    paramValue = Convert.ToString(value);
                }
            }
            public string asString
            {
                get
                {
                    return Convert.ToString(paramValue);
                }
                set
                {
                    paramValue = Convert.ToString(value);
                }
            }
        }

        public class ComponentParams : Dictionary<string, ComponentParam>
        {
            public int ParamAsInt(string name, int defaultValue)
            {
                return (ContainsKey(name)) ? this[name].asInt : defaultValue;
            }
            public int ParamAsInt(string name)
            {
                return ParamAsInt(name, 0);
            }
            public bool ParamAsBool(string name, bool defaultValue)
            {
                return (ContainsKey(name)) ? this[name].asBool : defaultValue;
            }
            public bool ParamAsBool(string name)
            {
                return ParamAsBool(name, false);
            }
            public string ParamAsString(string name, string defaultValue)
            {
                return (ContainsKey(name)) ? this[name].asString : defaultValue;
            }
            public string ParamAsString(string name)
            {
                return ParamAsString(name, "");
            }
        }

        protected virtual void InitilizeComponent()
        {
        }

        protected virtual void FinalizeComponent()
        {
        }

        protected virtual void DoStartUp()
        {
        }

        protected virtual void DoShutDown()
        {
        }

        public virtual void AssignParams(ComponentParams componentParams)
        {
        }

        private ComponentContext _context;
        public ComponentContext context { get { return _context; } }
        protected virtual ComponentContext CreateContext()
        {
            return new ComponentContext(this);
        }
        
        public Component()
        {
            _context = CreateContext();
            InitilizeComponent();
        }
        
        public Component(ComponentParams componentParams): this()
        {   
            AssignParams(componentParams);
        }

        public Component(bool NeedStartUp): this()
        {
            if (NeedStartUp)
                StartUp();
        }
        
        ~Component()
        {
            if (isStarted)
                ShutDown();
            FinalizeComponent();
        }

        public void StartUp()
        {
            if (!isStarted)
            {
                DoStartUp();
                _isStarted = true;
            }
        }

        public void ShutDown()
        {
            if (isStarted)
            {
                DoShutDown();
                _isStarted = false;
            }
        }

        public void Restart()
        {
            ShutDown();
            StartUp();
        }

        private bool _isStarted;
        public bool isStarted { 
            get 
            { 
                return _isStarted; 
            }
            set
            {
                if (value)
                    StartUp();
                else
                    ShutDown();
            }
        }

    }
}
